package com.blog.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blog.dao.BlogTypeMapper;
import com.blog.pojo.Blog;
import com.blog.pojo.Type;
import com.blog.service.BlogTypeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright (C), 2022
 * Author: 苏大大
 * Date: 2022-04-23 23:01
 * FileName: BlogTypeServiceImpl
 * Description: 博客分类业务层
 */
@Service
public class BlogTypeServiceImpl implements BlogTypeService {
    @Resource
    private BlogTypeMapper mapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public List<Type> queryTypeAll() {
        List<Type> CacheList=mapper.queryTypeAll();
        if(redisTemplate.opsForValue().get("TypeList")!=null&&redisTemplate.opsForValue().get("TypeList")==CacheList){
            String a=redisTemplate.opsForValue().get("TypeList").toString();
            List<Type> list=(List<Type>) JSON.parseArray(a,Type.class);
            return list;
        }
        if(CacheList!=null){
            //异常设备的json数组,把list转换为json形式数组
            JSONArray jsonArray= JSONObject.parseArray(JSONObject.toJSONString(CacheList));
            //将json形式数组装换为string类型
            String json= jsonArray.toJSONString();
            //将需要缓存的数据缓存入redis
            redisTemplate.opsForValue().set("TypeList",json);
        }
        String a=redisTemplate.opsForValue().get("TypeList").toString();
        List<Type> List=(List<Type>) JSON.parseArray(a,Type.class);

        return List;
    }
}
